<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 短信发送ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

class ajax_sms {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //发送手机短信
                    $this->sendSms();
                    break;
                case 2: //发送手机短信 新版不带验证码的
                    $this->sendSmsNoYzm();
                    break;
                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 发送手机短信
     */
    private function sendSms() {

        global $G_SHOP;

        //加载短信类
        quanli_include('module/base/common/quanliSms.class.php');

        //实例化短信类
        $quanliSms = new quanliSms();

        if (!isset($_REQUEST['mobile'], $_REQUEST['template']) || !is_mobile($_REQUEST['mobile']) || empty($_REQUEST['template'])) {
            returnJsonData('参数错误');
        }

        //检查图片验证码
        if (!isset($_REQUEST['yzm']) || empty($_REQUEST['yzm']))
            returnJsonData('验证码不能为空');
        else if (md5($_REQUEST['yzm']) != $G_SHOP->sessionCache_get('yzm'))
            returnJsonData('验证码错误', 1);
        $G_SHOP->sessionCache_remove('yzm');


        $mobile = $_REQUEST['mobile']; //手机号码
        $template = $_REQUEST['template']; //消息模板

        $current_time = time(); //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $mobile); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 60) {
            returnJsonData('手机短信已发送，请一分钟以后再试');
        }


        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $sms_yzm = $quanliSms->createSmsCode();

        //生成短信内容
        $data_array = array(
            '$sms_yzm' => $sms_yzm,
            '$mobile' => $mobile
        );
        $sms_content = $quanliSms->getSmsContent( $template, $data_array );
        if (!$sms_content) {
            returnJsonData('短信初始化失败', 404);
        }

        //发送短信
        $result_array = $quanliSms->sendSMS( $mobile, $sms_content );

        /**
         * ============================
         * 发送短信
         * ============================
         */

        if ($result_array['code'] == 200) {

            //保存短信验证码到redis
            $G_SHOP->sessionCache_set('sms_yzm_' . $mobile, md5($sms_yzm), 3600);//将验证码信息保存到memcache中去
            $G_SHOP->sessionCache_set('sms_yzm_time_' . $mobile, time(), 3600);

        }

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 发送手机短信 不用验证验证码
     */
    private function sendSmsNoYzm(){


        global $G_SHOP;

        quanli_require('module/base/customer/quanliCust.class.php');

        //实例会员类
        $quanliCust = new quanliCust();



        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['mobile']
        );


        $cust_array = $quanliCust->searchCustomerDetail( $arg_array );

        if ($cust_array) {

            returnJsonData('您已经是Mcake的会员，直接开始选购吧。', 404, array( 'type' => $cust_array['custType'] ));

        }

        //加载短信类
        quanli_include('module/base/common/quanliSms.class.php');

        //实例化短信类
        $quanliSms = new quanliSms();

        if (!isset($_REQUEST['mobile']) || !is_mobile($_REQUEST['mobile']) ) {
            returnJsonData('参数错误');
        }

        $mobile = $_REQUEST['mobile']; //手机号码

        $current_time = $_SERVER['REQUEST_TIME']; //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $mobile); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 60) {
            returnJsonData('手机短信已发送，请一分钟以后再试');
        }

        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $sms_yzm = $quanliSms->createSmsCode();

        //生成短信内容
        $data_array = array(
            '$sms_yzm' => $sms_yzm,
            '$mobile' => $mobile
        );
        $sms_content = $quanliSms->getSmsContent( $template, $data_array );
        if (!$sms_content) {
            returnJsonData('短信初始化失败', 404);
        }

        //发送短信
        $result_array = $quanliSms->sendSMS( $mobile, $sms_content );

        /**
         * ============================
         * 发送短信
         * ============================
         */

        if ($result_array['code'] == 200) {

            //保存短信验证码到redis
            $G_SHOP->sessionCache_set('sms_yzm_' . $mobile, md5($sms_yzm), 3600);//将验证码信息保存到memcache中去
            $G_SHOP->sessionCache_set('sms_yzm_time_' . $mobile, time(), 3600);

        }

        returnJsonData($result_array['message'], $result_array['code']);

    }

}